//
//  MRSearchDetailViewController.m
//  Travel_World
//
//  Created by dllo on 15/10/20.
//  Copyright © 2015年 TLS. All rights reserved.
//

#import "MRSearchDetailViewController.h"
#import "MRDestinationModel.h"
#import "MRHotel_TableViewCell.h"
#import "MRCountry_messageViewController.h"

@interface MRSearchDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *array;

@property (nonatomic, strong) NSMutableDictionary *dic;

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation MRSearchDetailViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createTableView];
    
    [self createData];
    
    self.view.frame = CGRectMake(0, 64, WIDTH, HEIGHT + 64);
    
}

#pragma mark 数据解析
- (void)createData
{
    NSString *searchText = [self.searchText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSString *url = [NSString stringWithFormat:@"http://open.qyer.com/qyer/hotel/search_list?checkin=%@00%%3A00%%3A01&checkout=%@23%3%A59%%3A59&city_id=%@&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&from_key=app_ios_city_search&hotel=%@&lat=38.88253901135159&lon=121.5398019256965&oauth_token=55d7f8a1931dedc7d5a8f24ab63426d0&orderby=1&page=1&track_app_channel=App%%2520Store&track_app_version=6.7&track_device_info=iPhone5%%2C4&track_deviceid=41AE760B-510C-4778-AF4D-177B387E0F3B&track_os=ios%%25209.0.2&track_user_id=6852801&v=1",self.inLabelText,self.outLabelText,self.Id,searchText];
    
    NSLog(@"self.Id =%@",self.Id);
    NSLog(@"self.searchText = %@",self.searchText);
    NSLog(@"searchText = %@",searchText);
    
    [QTAFNnetWorkTool getUrl:url body:nil hudSuperView:self.view result:QTJSON headerFile:nil success:^(id result) {
       
        self.dic = result[@"data"];
        
        self.array = [MRDestinationModel baseModelByArr:result[@"data"][@"hotel"]];
        
        [self.tableView reloadData];
        
    }];
}

#pragma mark 创建tableView
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.rowHeight = 100 * HEI;
}

#pragma mark tableView的cell的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

#pragma mark 创建tableView的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    MRHotel_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (self.array.count > indexPath.row) {
        if (!cell) {
            
            cell = [[MRHotel_TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
            
        }
        
        MRDestinationModel *model = self.array[indexPath.row];
        
        [cell.ImageView sd_setImageWithURL:[NSURL URLWithString:model.photo]placeholderImage:[UIImage imageNamed:@"placeholder"]];
        
        cell.title.text = model.cnname;
        cell.enTitle.text = model.enname;
        cell.star.text = model.star;
        cell.point.text = [NSString stringWithFormat:@"%@分",model.ranking.description];
        cell.local.text = model.area_name;
        
        
        //        NSString *priceStr = model.price.description;
        //        NSString *price = [priceStr substringWithRange:NSMakeRange(4, priceStr.length-11)];
        cell.price.text = [NSString stringWithFormat:@"%@元起",model.price.description];
        
        //        cell.discount.text = model.priceoff;
        //        cell.price.text = [NSString stringWithFormat:@"%@元起",model.price];
        
        return cell;
    } else {
        
        return nil;
        
    }
}

#pragma mark tableView的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.array.count > indexPath.row) {
        
        MRCountry_messageViewController *messVC = [[MRCountry_messageViewController alloc] init];
        
        messVC.url = [NSString stringWithFormat:@"%@",[self.array[indexPath.row] Link]];
        
        [self.navigationController pushViewController:messVC animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
